/*** This figure decomposes the change in personal consumption expenditures (PCE) in
	 the Great Recession and the COVID-19 Recession.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Spending"

* Create required subfolders
cap mkdir "${root}/results/`category'"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

*-------------------------------------------------------------------------------
* Load and process data
*-------------------------------------------------------------------------------

* Load NIPA Table 2.3.6
project, uses("${root}/data/derived/NIPA/NIPA Table 2.3.6M Real Personal Consumption Expenditures by Major Type of Product in Chained Dollars.dta")
use "${root}/data/derived/NIPA/NIPA Table 2.3.6M Real Personal Consumption Expenditures by Major Type of Product in Chained Dollars.dta", clear

* Keep major breakdowns of PCE
keep if inlist(serieslabel, "Personal consumption expenditures", "Durable goods", "Nondurable goods", "Services")
drop seriescode

* Generate Stata year-month variable
gen ym = ym(year, month)
format ym %tmMon_CCYY
drop year month

* Verify that Durables + Nondurables + Services approximately equals PCE
* 	Note: Due to BEA's chained dollar procedure, the components do not exactly
* 		  sum to the total. This is documented by BEA.

sum value if ym == ym(2020, 1) & serieslabel=="Personal consumption expenditures"
assert r(N) == 1
local pce_jan2020 = r(sum)

sum value if ym == ym(2020, 1) & serieslabel!="Personal consumption expenditures"
assert r(N)==3
local sum_jan2020 = r(sum)

di reldif(`pce_jan2020',`sum_jan2020')
assert reldif(`pce_jan2020',`sum_jan2020') < 0.01

* Look at Percent change GFC (Dec 2007 - June 2009) vs COVID (Jan 2020 - April 2020)
gen shock = "Great Recession" if inlist(ym, ym(2007,12), ym(2009,6))
replace shock = "COVID" if inlist(ym, ym(2020,1), ym(2020,4))
drop if mi(shock)

* Calculate peak to trough differences
order shock serieslabel ym
isid serieslabel ym

bys shock serieslabel (ym): gen diff = value - value[_n-1]

gen percent_share_of_pce = .
foreach shock in "Great Recession" "COVID" {
	sum diff if shock=="`shock'" & serieslabel != "Personal consumption expenditures", meanonly
	assert r(N) == 3
	replace percent_share_of_pce = diff / r(sum) * 100 if shock == "`shock'"
}

* Create the bar chart
keep if inlist(serieslabel, "Durable goods", "Nondurable goods", "Services")
sort ym serieslabel
keep if !mi(percent_share_of_pce)

gen x_axis = _n
replace x_axis = x_axis - 3 if x_axis > 3
replace x_axis = x_axis + 0.15 if _n > 3
replace x_axis = x_axis - 0.15 if _n <= 3

format percent_share_of_pce %3.1f
tostring percent_share_of_pce, gen(label) format("%3.1f") force
replace label = label + "%"

tw ///
	(bar percent_share_of_pce x_axis if ym == ym(2009,6), barw(0.3) color(oi3)) ///
	(bar percent_share_of_pce x_axis if ym != ym(2009,6), barw(0.3)) ///
	(scatter percent_share_of_pce x_axis if percent_share_of_pce > 0, ///
	mlabel(label) mlabsize(medium) msymb(none) mlabgap(1) mlabpos(12) mlabcolor(gs7)) ///
	(scatter percent_share_of_pce x_axis if percent_share_of_pce < 0, ///
	mlabel(label) mlabsize(medium) msymb(none) mlabgap(7) mlabpos(12) mlabcolor(gs7)) ///
	, ylab(0 "0%" 25 "25%" 50 "50%" 75 "75%", nogrid) xtitle("") ///
	ytitle("Share of Peak to Trough Decline in " "Personal Consumption Expenditure (%)") ///
	legend(order(1 "Great Recession" 2 "COVID-19"))  yline(0, lcolor(gs7)) ///
	xlab(1 "Durables" 2 "Non-Durables" 3 "Services", notick) ${title_`version'}
oi_graph_export "${root}/results/Spending/Spending Changes by Sector COVID vs GFC", type(${fig_type})

* Black and white version for QJE
tw ///
	(bar percent_share_of_pce x_axis if ym == ym(2009,6), barw(0.3) color(gs12)) ///
	(bar percent_share_of_pce x_axis if ym != ym(2009,6), barw(0.3) color(gs4)) ///
	(scatter percent_share_of_pce x_axis if percent_share_of_pce > 0, ///
	mlabel(label) mlabsize(medium) msymb(none) mlabgap(1) mlabpos(12) mlabcolor(gs7)) ///
	(scatter percent_share_of_pce x_axis if percent_share_of_pce < 0, ///
	mlabel(label) mlabsize(medium) msymb(none) mlabgap(7) mlabpos(12) mlabcolor(gs7)) ///
	, ylab(0 "0%" 25 "25%" 50 "50%" 75 "75%", nogrid) xtitle("") ///
	ytitle("Share of Peak to Trough Decline in " "Personal Consumption Expenditure (%)") ///
	legend(order(1 "Great Recession" 2 "COVID-19"))  yline(0, lcolor(gs7)) ///
	xlab(1 "Durables" 2 "Non-Durables" 3 "Services", notick) ${title_`version'}

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_1c.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_1c.svg")

*-------------------------------------------------------------------------------
* Export output numbers to csv file
*-------------------------------------------------------------------------------

cap erase "${root}/results/paper numbers/`category'/Spending Changes by Sector - COVID vs Great Recession.yaml"

sum percent_share_of_pce if serieslabel == "Durable goods" & shock == "Great Recession"
assert r(N) == 1
yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Sector - COVID vs Great Recession.yaml", ///
	key("dur_gfc") ///
	comment("Share of Peak to Trough Decline in Personal Consumption Expenditure (%) - Durables, Great Recession") ///
	value(`r(mean)') fmt(%9.0f)

sum percent_share_of_pce if serieslabel == "Durable goods" & shock == "COVID"
assert r(N) == 1
yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Sector - COVID vs Great Recession.yaml", ///
	key("dur_covid") ///
	comment("Share of Peak to Trough Decline in Personal Consumption Expenditure (%) - Durables, COVID") ///
	value(`=r(mean)') fmt(%9.0f)

sum percent_share_of_pce if serieslabel == "Nondurable goods" & shock == "Great Recession"
assert r(N) == 1
yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Sector - COVID vs Great Recession.yaml", ///
	key("nondur_gfc") ///
	comment("Share of Peak to Trough Decline in Personal Consumption Expenditure (%) - Non-Durables, Great Recession") ///
	value(`=r(mean)') fmt(%9.0f)

sum percent_share_of_pce if serieslabel == "Nondurable goods" & shock == "COVID"
assert r(N) == 1
yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Sector - COVID vs Great Recession.yaml", ///
	key("nondur_covid") ///
	comment("Share of Peak to Trough Decline in Personal Consumption Expenditure (%) - Non-Durables, COVID") ///
	value(`=r(mean)') fmt(%9.0f)

sum percent_share_of_pce if serieslabel == "Services" & shock == "Great Recession"
assert r(N) == 1
yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Sector - COVID vs Great Recession.yaml", ///
	key("serv_gfc") ///
	comment("Share of Peak to Trough Decline in Personal Consumption Expenditure (%) - Services, Great Recession") ///
	value(`=r(mean)') fmt(%9.0f)

sum percent_share_of_pce if serieslabel == "Services" & shock == "COVID"
assert r(N) == 1
yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Sector - COVID vs Great Recession.yaml", ///
	key("serv_covid") ///
	comment("Share of Peak to Trough Decline in Personal Consumption Expenditure (%) - Services, COVID") ///
	value(`=r(mean)') fmt(%9.0f)

project, creates("${root}/results/paper numbers/`category'/Spending Changes by Sector - COVID vs Great Recession.yaml")
